package server

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"github.com/swaggo/files"
	"github.com/swaggo/gin-swagger"
	"log"
	"my-job/web/controller"
)

type Server struct {
	Port int `json:"port"`
}

func (s *Server) Start() {
	log.Print("开始启动服务\n")
	r := gin.New()
	r.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))
	matterRouter(r)
	// r.Use() 使用全局中间件
	err := r.Run(s.getAddr())
	if err != nil {
		log.Fatalln("启动 web api 服务失败：" + err.Error())
	}
}

func (s *Server)getAddr() string{
	return fmt.Sprintf(":%d", s.Port)
}

func matterRouter(r *gin.Engine){
	group := r.Group("matter")
	matterController := new(controller.MatterController)
	group.GET("/:id", matterController.Get)
	group.PUT("/:id", matterController.Modify)
	group.DELETE("/:id", matterController.Delete)
	group.PUT("/:id/finish", matterController.Finish)
	group.GET("/", matterController.GetList)
	group.POST("/", matterController.Add)
	group.PUT("/:id/tags", matterController.AddTag)
	return
}